//
//  PeripheralDelegate.swift
//  Nexs
//
//  Created by roni on 2025/7/30.
//

import Foundation
import CoreBluetooth

// MARK: - 外设管理器代理协议
/// 蓝牙外设管理器代理协议，用于接收广播和连接相关的事件回调
public protocol BluetoothPeripheralManagerDelegate: AnyObject {
    
    /// 外设广播状态发生变化
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - state: 新的广播状态
    func peripheralManager(_ manager: BluetoothPeripheralManager, didChangeState state: PeripheralBroadcastState)
    
    /// 成功开始广播
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - configuration: 广播配置信息
    func peripheralManager(_ manager: BluetoothPeripheralManager, didStartBroadcasting configuration: BroadcastConfiguration)
    
    /// 广播已停止
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - statistics: 本次广播的统计信息
    func peripheralManager(_ manager: BluetoothPeripheralManager, didStopBroadcasting statistics: BroadcastStatistics)
    
    /// 广播操作失败
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - error: 失败的错误信息
    func peripheralManager(_ manager: BluetoothPeripheralManager, didFailToBroadcast error: PeripheralError)
    
    /// 中央设备连接到此外设
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - central: 连接的中央设备
    func peripheralManager(_ manager: BluetoothPeripheralManager, didReceiveConnectionFrom central: CBCentral)
    
    /// 中央设备从此外设断开连接
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - central: 断开连接的中央设备
    func peripheralManager(_ manager: BluetoothPeripheralManager, didDisconnectFrom central: CBCentral)
    
    /// 收到中央设备的读取请求
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - request: 读取请求对象
    func peripheralManager(_ manager: BluetoothPeripheralManager, didReceiveReadRequest request: CBATTRequest)
    
    /// 收到中央设备的写入请求
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - requests: 写入请求数组
    func peripheralManager(_ manager: BluetoothPeripheralManager, didReceiveWriteRequests requests: [CBATTRequest])
    
    /// 外设准备向订阅的中央设备发送数据更新
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - characteristic: 要更新的特征
    func peripheralManagerIsReady(_ manager: BluetoothPeripheralManager, toUpdateSubscribers characteristic: CBCharacteristic)
    
    /// 蓝牙管理器状态发生变化
    /// - Parameters:
    ///   - manager: 外设管理器实例
    ///   - state: CBManager状态
    ///   - message: 状态描述信息
    func peripheralManager(_ manager: BluetoothPeripheralManager, didChangeCentralManagerState state: CBManagerState, message: String)
}

// MARK: - 代理方法默认实现
/// 为代理协议提供默认的空实现，使所有方法都变为可选
public extension BluetoothPeripheralManagerDelegate {
    func peripheralManager(_ manager: BluetoothPeripheralManager, didChangeState state: PeripheralBroadcastState) {}
    func peripheralManager(_ manager: BluetoothPeripheralManager, didStartBroadcasting configuration: BroadcastConfiguration) {}
    func peripheralManager(_ manager: BluetoothPeripheralManager, didStopBroadcasting statistics: BroadcastStatistics) {}
    func peripheralManager(_ manager: BluetoothPeripheralManager, didFailToBroadcast error: PeripheralError) {}
    func peripheralManager(_ manager: BluetoothPeripheralManager, didReceiveConnectionFrom central: CBCentral) {}
    func peripheralManager(_ manager: BluetoothPeripheralManager, didDisconnectFrom central: CBCentral) {}
    func peripheralManager(_ manager: BluetoothPeripheralManager, didReceiveReadRequest request: CBATTRequest) {}
    func peripheralManager(_ manager: BluetoothPeripheralManager, didReceiveWriteRequests requests: [CBATTRequest]) {}
    func peripheralManagerIsReady(_ manager: BluetoothPeripheralManager, toUpdateSubscribers characteristic: CBCharacteristic) {}
    func peripheralManager(_ manager: BluetoothPeripheralManager, didChangeCentralManagerState state: CBManagerState, message: String) {}
} 
